//
//  OneChatVipSetManager.m
//  chatModule
//
//  Created by sy on 2025/5/22.
//

#import "OneChatVipSetManager.h"

#import "OneChatUserConfigService.h"
#import "OneChatPriceListService.h"
#import "OneChatConfigOrderService.h"
#import "OneChatUpdateUserConfigService.h"
#import <BGFMDB/BGFMDB.h>
#import "MSUserManager.h"

@implementation OneChatVipSetManager

+ (void)getChatUserConfig:(void(^)(OneChatSetModel*chatSetModel))successBlock withError:(nullable chatFailureError)failure{

   NSArray *tampArr = [OneChatSetModel bg_find:chatSetTableName where:[NSString stringWithFormat:@"where %@=%@ ",bg_sqlKey(@"account"),bg_sqlValue([MSUserManager sharedManager].userModel.account)]];
    if (tampArr.count >0) {
        successBlock(tampArr.firstObject);
    }
    
    OneChatUserConfigService *userConfig = [[OneChatUserConfigService alloc]init];
    userConfig.requstType = Requstget;
    
    [userConfig requst:^(id  _Nullable responseObject) {
        OneChatSetModel*chatSetModel = [OneChatSetModel mj_objectWithKeyValues:responseObject[@"data"]];
        chatSetModel.account = [MSUserManager sharedManager].userModel.account;

        chatSetModel.bg_tableName = chatSetTableName;//自定义数据库表名称(库自带的字段).
        [chatSetModel bg_saveOrUpdate];
        
        successBlock(chatSetModel);
    } fail:^(NSError * _Nullable error, NSString * _Nullable errorStr) {
        failure(error);
    }];
}

+ (void)updateChatUserConfig:(NSDictionary *)params withResult:(void (^)(BOOL))successBlock withError:(chatFailureError)failure{
    
    OneChatUpdateUserConfigService *userConfig = [[OneChatUpdateUserConfigService alloc]init];

    userConfig.params = params;
    userConfig.requstType = Requstget;
    
    [userConfig requst:^(id  _Nullable responseObject) {
        if ([responseObject[@"code"]intValue]==200) {
            successBlock(YES);
        }
    } fail:^(NSError * _Nullable error, NSString * _Nullable errorStr) {
        failure(error);
    }];
}
+ (void)getChatPriceList:(void (^)(NSArray<OneChatVipPriceModel *> * _Nonnull))successBlock withError:(chatFailureError)failure{
    OneChatPriceListService *listService = [[OneChatPriceListService alloc]init];
    listService.requstType = Requstget;
    [listService requst:^(id  _Nullable responseObject) {
        if ([responseObject[@"code"]intValue]==200) {
            NSArray *listArr = [OneChatVipGroupModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"group_list"]];
            successBlock(listArr);
        }
    } fail:^(NSError * _Nullable error, NSString * _Nullable errorStr) {
        failure(error);
    }];
}

+ (void)updateConfigOrder:(nonnull NSString*)price_id withResult:(void(^)(NSArray*priceList))successBlock withError:(nullable chatFailureError)failure{
    OneChatConfigOrderService *orderServic = [[OneChatConfigOrderService alloc]init];
    orderServic.requstType = Requstget;
    orderServic.price_id = price_id;
    
    [orderServic requst:^(id  _Nullable responseObject) {
        
    } fail:^(NSError * _Nullable error, NSString * _Nullable errorStr) {
        
    }];
}

+ (id)convertDictionary:(NSDictionary *)dict toObjectOfClass:(Class)cls {
    id object = [[cls alloc] init];
    
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList(cls, &count);
    
    for (int i = 0; i < count; i++) {
        NSString *propertyName = [NSString stringWithUTF8String:property_getName(properties[i])];
        id value = dict[propertyName];
        
        if (value && ![value isKindOfClass:[NSNull class]]) {
            @try {
                [object setValue:value forKey:propertyName];
            } @catch (NSException *exception) {
                NSLog(@"设置属性 %@ 失败: %@", propertyName, exception.reason);
            }
        }
    }
    free(properties);
    
    return object;
}


@end
